package com.xpgk.mvc.warning.continuedProductCustomer;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.template.ext.directive.Str;
import com.xpgk.mvc.base.BaseController;
import com.xpgk.mvc.bentryRegister.CustomerDetail;
import com.xpgk.mvc.bentryRegister.CustomerProduct;
import com.xpgk.mvc.binShopService.CustomerService;
import com.xpgk.mvc.codeSet.Code;
import com.xpgk.mvc.productSet.Product;
import com.xpgk.mvc.statistic.stockHandoverStatistic.StockHandoverStatisticSQL;
import com.xpgk.mvc.user.User;
import com.xpgk.tools.ToolDateTime;

public class ContinuedProductCustomerController extends BaseController{

	public void index() {
		
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("column", Product.column_useIds);
		String sql = getSqlByBeetl(Product.sqlId_column, param);
		List<Product> products = Product.dao.find(sql, Code.cpytFw_ids);
		List<Code> parts = Code.cacheGetChild(Code.fwbw_ids);
		setAttr("products", products);
		render("continuedProductCustomer.html");
		//e6f913b5b3bd4d9a8c50ab24093e7071
	}
	public void view() {

	
		String startTime = getPara("startTime", ToolDateTime.getMonthOneDay(new Date()));
		String organIds = getPara("organIds", getCUserOrganIds());
		String sycs = getPara("sycs");
		List<Record> result = new ArrayList<>();
//		
//		
//		Map<String, Object> param = new HashMap<String, Object>();
//		param.put("column", Product.column_useIds);
//		String sql = getSqlByBeetl(Product.sqlId_column, param);
//		List<Product> products = Product.dao.find(sql, Code.cpytFw_ids);//产品
//		if(products.size() > 0){
//			for(Product product : products){
				if(sycs == null || sycs.equals("")){
					sycs= Code.dao.findById("e6f913b5b3bd4d9a8c50ab24093e7071").getStr("code");
				}
				String sqlSer = "SELECT * from  sb_customer_service WHERE customerID = ? and dataStatus in ('1','2') ORDER BY startTime desc";
				String sql = "select * from sb_customer_detail WHERE organIds = ? and dataStatus in ('1','2')";
				String cpSql = "select * from sb_customer_product WHERE  customerID = ? and productID = ?  and totalExperience<=?  and dataStatus in ('1','2')";
				String sql_sycs = getSqlMy(ContinuedProductCustomerSQL.sqlId_continuedProductCustomer);
				List<CustomerDetail> cusList = CustomerDetail.dao.find(sql,organIds);
				if(cusList!=null && cusList.size()>0){
					for(CustomerDetail cdet : cusList){
						String cusIds = cdet.getStr(CustomerDetail.column_customerIds);
						CustomerProduct g = CustomerProduct.dao.findFirst(cpSql,cusIds,"daaba791f4104ae98504a1bc421e0d8d",sycs);
						CustomerProduct jy = CustomerProduct.dao.findFirst(cpSql,cusIds,"95e2ae17462e4d0eb1dc84bc63bc478a",sycs);
						if(g!=null || jy!=null){
							Record r = new Record();
							r.set("customerIds", cusIds)
							 .set("customer_name",cdet.getStr(CustomerDetail.column_name))
							 .set("firstDate", ToolDateTime.format(cdet.getDate(CustomerDetail.column_firstDate), ToolDateTime.pattern_ymd))
							 .set("firstWeight", cdet.getFloat(CustomerDetail.column_firstWeight))
							 .set("targetWeight", cdet.getFloat(CustomerDetail.column_targetWeight))
							 .set("nowWeight", cdet.getFloat(CustomerDetail.column_nowWeight))
							 .set("wjjs", new DecimalFormat("0.0").format(cdet.getFloat(CustomerDetail.column_nowWeight)-cdet.getFloat(CustomerDetail.column_targetWeight)));
						
							if(g!=null && g.getFloat(CustomerProduct.column_totalExperience)!=null){
								r.set("_amggm",  g.getFloat(CustomerProduct.column_totalExperience));
							}
							if(jy!=null && jy.getFloat(CustomerProduct.column_totalExperience)!=null){
								r.set("_jysy",  jy.getFloat(CustomerProduct.column_totalExperience));
							}
							CustomerService cs = CustomerService.dao.findFirst(sqlSer,cusIds);
							if(cs!=null){
								r.set("lastTime", ToolDateTime.format(cs.getDate(CustomerService.column_startTime), ToolDateTime.pattern_ymd));
							}else{
								r.set("lastTime",  ToolDateTime.format(cdet.getDate(CustomerDetail.column_firstDate), ToolDateTime.pattern_ymd));
							}
							result.add(r);
							
						}
					}
				}
			/*	List<Record> records = Db.find(sql_sycs,organIds,sycs,organIds,sycs,organIds,sycs,organIds,sycs);
				//String sql = "select * from sb_customer_detail WHERE customerIds = ? and dataStatus in ('1','2')";
				
				if(records!=null){
					
					for (int i = 0; i < records.size(); i++) {
						Record r = new Record();
						r.set("_amggm", records.get(i).getFloat("amgcs"));
						r.set("_amgsy", records.get(i).getFloat("amgks"));
						r.set("_jygm", records.get(i).getFloat("jyks"));
						r.set("_jysy", records.get(i).getFloat("jycs"));
						r.set("customer_name", records.get(i).getStr("customer_name"));
						r.set("phone", records.get(i).getStr("phone"));
						r.set("customerIds", records.get(i).getStr("customerIds"));
						CustomerDetail cdet = CustomerDetail.dao.findFirst(sql, records.get(i).getStr("customerIds"));
						if(cdet!=null){
							r.set("firstDate", ToolDateTime.format(cdet.getDate(CustomerDetail.column_firstDate), ToolDateTime.pattern_ymd))
							 .set("firstWeight", cdet.getFloat(CustomerDetail.column_firstWeight))
							 .set("targetWeight", cdet.getFloat(CustomerDetail.column_targetWeight))
							 .set("nowWeight", cdet.getFloat(CustomerDetail.column_nowWeight))
							 .set("wjjs", new DecimalFormat("0.0").format(cdet.getFloat(CustomerDetail.column_nowWeight)-cdet.getFloat(CustomerDetail.column_targetWeight)));
						}
						CustomerService cs = CustomerService.dao.findFirst(sqlSer,records.get(i).getStr("customerIds"));
						if(cs!=null){
							r.set("lastTime", ToolDateTime.format(cs.getDate(CustomerService.column_startTime), ToolDateTime.pattern_ymd));
						}
						result.add(r);
					}*/
					
					/*for(Record continuedProductCustomer:continuedProductCustomerS){
						Record r = new Record();
						int i = 1;
						r.set(product.getStr("ids")+i+ "_gm", continuedProductCustomer.getFloat("ks"));
						r.set(product.getStr("ids")+i+ "_sy", continuedProductCustomer.getFloat("cs"));
						
						
						r.set("customer_name", continuedProductCustomer.getStr("customer_name"));
						
						r.set("phone", continuedProductCustomer.getStr("phone"));
						i++;
						result.add(r);
					}*/
					
				//}
				
		//	}
		//}
	
		String json = JsonKit.toJson(result);
		renderJson(json);
	}
	
}
